#pragma once
#include "Xps/Protocol/IProtocol.h"
#include "Xps/Object/IObjectFactory.h"

namespace Xps
{
    namespace UDP
    {
        class Multicast : 
            public IProtocol
        {
        public:
            Multicast(const std::string &_Name, const ip::address &_ListenAddress, const ip::address &_MulticastAddress, const short &_Port, const IObjectFactory *_ObjectFactory, const bool &_IsLoopBackEnabled = false);

            ~Multicast(void);

            virtual const std::string & getName() const;

            virtual void Start();

            virtual void Stop();

            virtual void Send( IPacket::Ptr _Packet );

        private:
            void Initialize();
            void Recieve();
            void Sender();
            void ProcessPackets();

            std::string name;
            bool is_running;
            bool is_loopback;
            boost::thread m_reciever;
            boost::thread th_sender;
            boost::thread th_process_packets;
            boost::shared_ptr<asio::io_service> io_service;
            boost::shared_ptr<udp::socket> socket;
            udp::endpoint multicast_endpoint;
            udp::endpoint listen_endpoint;
            concurrent_out_queue outgoing_queue;
            concurrent_in_queue incomming_queue;
            HANDLE recieve_event;
            HANDLE sender_event;
            const IObjectFactory *object_factory;
        };
    };
};
